

using WooEngine.Utility.Security.Base;

namespace WooEngine.Utility.Random{
	public class RTGameRandom : RTGameRandomBase {
		
		private long gameSeed;
		
		public RTGameRandom(long seed) : base(seed){
			gameSeed = seed;
		}
		
		public bool Hit(int possibility, int denominator) {
			int num = RandomInt (denominator);
			if (num <= possibility) {
				return true;
			}
			return false;
		}
		
		public float RandomFloat(int max) {
			return NextFloat() * max;
		}
		
		public float RandomFloat(float max) {
			return NextFloat() * max;
		}
		
		public int RandomRange(int min, int max) {
			if (min == max)
				return min;
			return RandomInt(max - min) + min;
		}
		
		public float RandomFloatRange(int min, int max) {
			return RandomFloat(max - min) + min;
		}
		
		public float RandomFloatRange(float min, float max) {
			return RandomFloat(max - min) + min;
		}

		public int RandomInt(int max) {
			if (max == 0)
				return 0;
			return NextInt(max + 1);
		}

		public EncryptInt RandomByWeights(params EncryptInt[] weights)
		{
			int len = weights.Length;
			int sum = 0;
			foreach (int n in weights)
			{
				sum += n;
			}
			if (sum <= 0)
			{
				return -1;
			}
			int rand = NextInt(sum);
			int m = 0;
			int i = 0;
			do
			{
				m += weights[i];
				if (rand < m)
					return i;
				i++;
			} while (i < len);
			return len - 1;
		}

		public int RandomByWeights(params int[] weights) {
			int len = weights.Length;
			int sum = 0;
			foreach (int n in weights) {
				sum += n;
			}
			if (sum <= 0) {
				return -1;
			}
			int rand = NextInt(sum);
			int m = 0;
			int i = 0;
			do {
				m += weights[i];
				if (rand < m)
					return i;
				i++;
			} while (i < len);
			return len - 1;
		}
		
		public int RandomByWeightsAndExcepts(int[] excepts, params int[] weights)
		{
			for (int i = 0; i < excepts.Length; ++i) {
				weights[excepts[i]] = 0;
			}
			return RandomByWeights(weights);
		}


		public int RandomByWeightsAndExcepts(int[] excepts, params EncryptInt[] weights)
		{
			for (int i = 0; i < excepts.Length; ++i)
			{
				weights[excepts[i]] = 0;
			}
			return RandomByWeights(weights);
		}

		public int RandomByWeights(int sum, params int[] weights) {
			int len = weights.Length;
			int rand = NextInt(sum);
			int m = 0;
			int i = 0;
			do {
				m += weights[i];
				if (rand < m)
					return i;
				i++;
			} while (i < len);
			return -1;
		}

		public long GetGameSeed() {
			return gameSeed;
		}

		public void SetGameSeed(long gameSeed) {
			this.gameSeed = gameSeed;
			this.SetSeed(gameSeed);
		}

	}
}